package dao;

import org.apache.ibatis.annotations.*;
import pojo.Sc;

import java.util.List;

@Mapper
/**
 * 收藏表数据访问接口
 */
public interface ScMapper {
    // 根据id查询收藏
    @Select("SELECT id, yh_id, wz_id, create_time FROM sc WHERE id = #{id}")
    Sc find_by_id(Integer id);

    // 查询用户的收藏（关联文章表）
    @Select("SELECT sc.id, sc.yh_id, sc.wz_id, sc.create_time, wz.title, wz.category " +
            "FROM sc JOIN wz ON sc.wz_id = wz.id " +
            "WHERE sc.yh_id = #{yh_id} " +
            "ORDER BY sc.create_time DESC")
    List<Sc> find_by_user_id(Integer yh_id);

    // 查询文章的收藏数
    @Select("SELECT COUNT(*) FROM sc WHERE wz_id = #{wz_id}")
    int count_by_wz_id(Integer wz_id);

    // 新增收藏
    @Insert("INSERT INTO sc (yh_id, wz_id) VALUES (#{yhId}, #{wzId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(Sc sc);

    // 删除收藏
    @Delete("DELETE FROM sc WHERE id = #{id}")
    int delete_by_id(Integer id);

    // 删除用户对文章的收藏
    @Delete("DELETE FROM sc WHERE yh_id = #{yhId} AND wz_id = #{wzId}")
    int delete_by_user_and_wz(Integer yhId, Integer wzId);
}